//
//  CQNewFeatureController.swift
//  swift-weibo
//
//  Created by 陈乾 on 15/10/29.
//  Copyright © 2015年 陈乾. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class CQNewFeatureController: UICollectionViewController {
    
    
    //创建一个流水布局模式
    private var layout = UICollectionViewFlowLayout()
    
    init() {
        super.init(collectionViewLayout: layout)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //设置
        setFeature()
        self.collectionView!.registerClass(CQNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }
    
    //MARK: - 设置属性
    func setFeature() {
        
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //水平方向滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //分页
        collectionView?.pagingEnabled = true
        //没有弹簧效果
        collectionView?.bounces = false
        
        
    }
    
    
    //MARK: - 数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? CQNewFeatureCell
        
        //        cell.backgroundColor = UIColor.redColor()
        cell?.imageIndex = indexPath.item
        
        return cell!
        
    }
    //MARK: - 代理方法 cell显示完毕调
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //正在显示的cell的indexPath
        let showIndexPath = collectionView.indexPathsForVisibleItems().first!
        //获取正在显示的cell
        let cell = collectionView.cellForItemAtIndexPath(showIndexPath) as? CQNewFeatureCell
        
        //如果是最后一页 按钮需要动画 
        if showIndexPath.item == 3 {
         
//            开始旋转
            cell?.startButtonAnimation()
        }
    }
    
    
    /***end***/
}

class CQNewFeatureCell: UICollectionViewCell {
    
    var imageIndex: Int = 0 {
        didSet{
           //设置图片
           cellImage.image = UIImage(named: "new_feature_\(imageIndex+1)")
            //在返回cell之前都要到这里来 我们把他隐藏 重用按钮出现的问题也解决了
            startButton.hidden = true
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    //MARK: - 准备UI
    func prepareUI() {
        //添加子控件
        contentView.addSubview(cellImage)
        contentView.addSubview(startButton)
        //约束干预取出
        cellImage.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        //设置约束
        //图片的约束
        // VFL
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg" : cellImage]))
        
        contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg" : cellImage]))
        
        // 开始体验按钮
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
   
    
    }
    
    
 
    //MARK: - 按钮动画
    func startButtonAnimation()
    {
        startButton.hidden = false
        // 把按钮的 transform 缩放设置为0
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    
    //MARK: - 按钮点击事件
    func btnClick() {
     
        //跳转首页控制器
        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchVC(true)
    
    }
    
    //MARK: - 懒加载
    lazy var cellImage: UIImageView = UIImageView()
    
    lazy var startButton: UIButton = {
        
        let btn = UIButton()
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        //设置文字
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        //按钮监听时间
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        }()
    
    /***end***/
}


